package com.phonebrand.adater;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.android.navigationdrawerexample.R;
import com.phonebrand.constant.PhoneBrandConstant;
import com.phonebrand.dao.DB4CommandDao;
import com.phonebrand.global.GlobalApplication;
import com.phonebrand.model.PhoneM;
import com.phonebrand.service.PhoneService;

public class AdapterTelListViewData extends BaseAdapter{
	private static final String LOG_TAG = "AdapterTelListViewData";
    private LayoutInflater mInflater;
    private Context context; //รับ Context จา�? CustomListViewActivity
    private ArrayList<PhoneM> listData = new ArrayList<PhoneM>(); //list ใน�?ารเ�?็บข้อมูลของ DataShow
    private String mode;
    DB4CommandDao dao;
    
    public AdapterTelListViewData(Context context,ArrayList<PhoneM> listData ,String mode) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.listData = listData;
        this.mode = mode;
        dao = DB4CommandDao.getInstance();
    }
 
    public int getCount() {
        // TODO Auto-generated method stub
        return listData.size(); //ส่งขนาดของ List ที่เ�?็บข้อมุลอยู่
    }
 
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }
 
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
 
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final HolderTelListAdapter holderListAdapter; //เ�?็บส่วนประ�?อบของ List �?ต่ละอัน
  /*
        if(convertView == null)
        {
    */
            //ใช้ Layout ของ List เราเราสร้างขึ้นเอง (convertView.xml)
            convertView = mInflater.inflate(R.layout.phonelist, null);
 
             //สร้างตัวเ�?็บส่วนประ�?อบของ List �?ต่ละอัน
            holderListAdapter = new HolderTelListAdapter();
 
            //เชื่อมส่วนประ�?อบต่างๆ ของ List เข้า�?ับ View
            holderListAdapter.txtName = (TextView) convertView.findViewById(R.id.txtName);
            holderListAdapter.txtPhone = (TextView) convertView.findViewById(R.id.txtPhone);             
            holderListAdapter.txtBrand = (TextView) convertView.findViewById(R.id.txtBrand);
            holderListAdapter.loadNetwork = (ImageView) convertView.findViewById(R.id.loadBrand);
            holderListAdapter.callType = (ImageView) convertView.findViewById(R.id.typeNum);
    /*  
    }else{
            holderListAdapter = (HolderTelListAdapter) convertView.getTag();
        } 
 */
        //ดึงข้อมูลจา�? listData มา�?สดงทีละ position
        
        holderListAdapter.txtName.setText(checkNull(listData.get(position).getName()));
        holderListAdapter.txtPhone.setText(listData.get(position).getPhoneNum());   
        holderListAdapter.txtBrand.setText(listData.get(position).getBrandName());
                

        if ("DTAC".equalsIgnoreCase(listData.get(position).getBrandName())) {
       	 holderListAdapter.txtBrand.setTextColor(Color.rgb(26, 187, 216));
       } else if ("AIS".equalsIgnoreCase(listData.get(position).getBrandName())) {
       	 holderListAdapter.txtBrand.setTextColor(Color.rgb(50, 180, 2));
       }else if ("TRUEMOVE".equalsIgnoreCase(listData.get(position).getBrandName())) {
      	 holderListAdapter.txtBrand.setTextColor(Color.rgb(246, 165, 26));
       }
       
            
        if (PhoneBrandConstant.MODELOG.equals(mode)) {
        	holderListAdapter.callType.setVisibility(View.VISIBLE);
        	if ("1".equals(listData.get(position).getType())) {         	
        		holderListAdapter.callType.setImageResource(R.drawable.arrow_income);
        	} else if ("2".equals(listData.get(position).getType())) {
        		holderListAdapter.callType.setImageResource(R.drawable.arrow_outgoing);
        	} else if ("3".equals(listData.get(position).getType())) {
        		holderListAdapter.callType.setImageResource(R.drawable.arrow_miss);
        	}
        	
        	if (listData.get(position).getCountNUm() > 1) {
        		holderListAdapter.txtName.setText(listData.get(position).getName()+ "  ( "+listData.get(position).getCountNUm()+" )");
        	}
        	
        }
                  
        
        //if (PhoneBrandConstant.LOADBRAND.equalsIgnoreCase(mode)) {
   
        	if (listData.get(position).getBrandName() == null || "".equalsIgnoreCase(listData.get(position).getBrandName().toString())) {
        		holderListAdapter.loadNetwork.setVisibility(View.VISIBLE);
        		holderListAdapter.txtBrand.setVisibility(View.GONE);
        	
        		holderListAdapter.loadNetwork.setOnClickListener(new OnClickListener() {        				
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						String brand = PhoneService.getInstance().getBrand(listData.get(position).getPhoneNum().replace("+66", "0"));
						PhoneM phoneM  = dao.loadPhoneMByNum(listData.get(position).getPhoneNum(),((GlobalApplication)context.getApplicationContext()).getDbPhone());
						if ( phoneM != null &&  !"".equals(brand) && brand != null) {
							phoneM.setBrandName(brand);
							dao.storePhone(phoneM,((GlobalApplication)context.getApplicationContext()).getDbPhone());
							((GlobalApplication)context.getApplicationContext()).getDbPhone().commit();
							
							holderListAdapter.loadNetwork.setVisibility(View.GONE);
			        		holderListAdapter.txtBrand.setVisibility(View.VISIBLE);
			        		holderListAdapter.txtBrand.setText(brand);
			        		listData.get(position).setBrandName(brand);
						}else {
							holderListAdapter.loadNetwork.setVisibility(View.GONE);
			        		holderListAdapter.txtBrand.setVisibility(View.VISIBLE);
			        		holderListAdapter.txtBrand.setText(brand);
			        		listData.get(position).setBrandName(brand);
							
						}
						
						

				        if ("DTAC".equalsIgnoreCase(listData.get(position).getBrandName())) {
				       	 holderListAdapter.txtBrand.setTextColor(Color.rgb(26, 187, 216));
				       } else if ("AIS".equalsIgnoreCase(listData.get(position).getBrandName())) {
				       	 holderListAdapter.txtBrand.setTextColor(Color.rgb(50, 180, 2));
				       }else if ("TRUEMOVE".equalsIgnoreCase(listData.get(position).getBrandName())) {
				      	 holderListAdapter.txtBrand.setTextColor(Color.rgb(246, 165, 26));
				       }
				       
						
					}
				});
				
       		
        	}
     
       // }
                 
        convertView.setTag(holderListAdapter);        
        
        //ถ้าทำ�?ารเลือ�?ที่ List จะ�?สดงข้อความ ว่าทำ�?ารเลือ�?ที่ List ที่เท่าไร
        convertView.setOnClickListener(new OnClickListener() {
 
            public void onClick(View v) {
            	   try {
					   String telNum = (String)listData.get(position).getPhoneNum();
					   telNum = "tel:"+telNum;
					   Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(telNum));
					   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					   context.startActivity(intent);
				   }catch (Exception e) {
					   e.printStackTrace();
				   }
            }
        });
                                 

        return convertView;
    }
    
    private String checkNull(String input) {
    	if (input == null || input.trim().equalsIgnoreCase("null")) {
    		return "";
    	} else {
    		return input;	
    	}
    	
    	
    }
}
